home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Dev / misc / temgen.lha / Temgen / tg-0.11 / txttab.c < prev    next >
C/C++ Source or Header  |  2002-12-18  |  3KB  |  120 lines

  1. #include "alloc.h"
  2. #include "sysdefs.h"
  3. #include "txttab.h"
  4.  
  5. struct txttab {
  6.     char **data;
  7.     int  size, delta;
  8.     char *buf;
  9.     int  bufsize, buflen;
  10. };
  11.  
  12. struct txttab *new_txttab( int size, int delta )
  13. {
  14.     struct txttab *t;
  15.     
  16.     if ( size < 0 || delta < 0 ) return NULL;
  17.     
  18.     t = (struct txttab*)MALLOC( sizeof(*t) );
  19.     if ( t ) {
  20.         t->size = size;
  21.         t->delta = delta;
  22.         t->buf = NULL;
  23.         t->bufsize = t->buflen = 0;
  24.         if ( size ) {
  25.             t->data = (char**)CALLOC( size, sizeof(char*) );
  26.             if ( !t->data ) {
  27.                 FREE( t );
  28.                 return NULL;
  29.             }
  30.         }
  31.     }
  32.     
  33.     return t;
  34. }
  35.  
  36. void free_txttab( struct txttab *t )
  37. {
  38.     int i;
  39.     
  40.     if ( t ) {
  41.         if ( t->data ) {
  42.             for ( i=0; i<t->size; i++ ) 
  43.                 if ( t->data[ i ] ) FREE( t->data[ i ] );
  44.             FREE( t->data );
  45.         }
  46.         
  47.         FREE( t );
  48.     }
  49. }
  50.  
  51. int tt_token( struct txttab *t, const char *s )
  52. {
  53.     int len;
  54.     if ( !(t && s) ) return -1;
  55.     
  56.     len = strlen( s );
  57.     if ( len + t->buflen >= t->bufsize ) {
  58.         int newsize = len + t->buflen + 256;
  59.         char *old = t->buf;
  60.         t->buf = (char*)REALLOC( old, newsize );
  61.         if ( !t->buf ) {
  62.             t->buf = old;
  63.             return -1;
  64.         }
  65.         t->bufsize = newsize;
  66.     }
  67.     
  68.     strcpy( t->buf + t->buflen, s );
  69.     t->buflen += len;
  70.     return 0;
  71. }
  72.  
  73. int tt_store( struct txttab *t, int line )
  74. {
  75.     if ( !t ) return -1;
  76.     
  77.     if ( t->size <= line ) {
  78.         char **old;
  79.         int s1, s2;
  80.         s1 = t->size + t->delta;
  81.         s2 = line+1;
  82.         s1 = (s1>s2) ? s1: s2;
  83.         old = t->data;
  84.         t->data = (char**)REALLOC( old, s1 * sizeof(char*) );
  85.         if ( !t->data ) {
  86.             t->data = old;
  87.             return -1;
  88.         }
  89.         memset( t->data+t->size, 0, (s1-t->size) * sizeof(t->data[0]) );
  90.         t->size = s1;
  91.     }
  92.     
  93.     if ( t->data[ line ] ) FREE( t->data[ line ] );
  94.     t->data[ line ] = STRDUP( t->buf ? t->buf: "" );
  95.     if ( t->bufsize > 512 ) {
  96.         char *old;
  97.         old = t->buf;
  98.         t->buf = (char*)REALLOC( t->buf, 512 );
  99.         if ( !t->buf ) 
  100.             t->buf = old;
  101.         else {
  102.             t->bufsize = 512;
  103.         }
  104.     }
  105.     
  106.     if ( t->buf ) t->buf[ 0 ] = '\0';
  107.     t->buflen = 0;
  108.     
  109.     return 0;
  110. }
  111.  
  112. const char *tt_find( struct txttab *t, int line )
  113. {
  114.     if ( !t ) return NULL;
  115.     if ( line < 0 || line >= t->size ) return NULL;
  116.     return t->data[ line ];
  117. }
  118.  
  119.  
  120.